package Cart_Servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import Cart_Function.Cart_Function;
import Cart_Utils.Cart;

/**
 * Servlet implementation class AddGood_Servlet
 */
@WebServlet("/AddGood_Servlet")
public class AddGood_Servlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AddGood_Servlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setContentType("text/html;charset=utf-8");
		
		String user = (String) request.getSession().getAttribute("UserName");
		String name = new String(request.getParameter("name").getBytes("iso-8859-1"),"utf-8");
		String num = request.getParameter("num");
		String msg;
		
		if(user==null){
			msg = "请先登录！";
		}else{
			Cart c = new Cart_Function().creatCart(name,num,user);
			
			if(new Cart_Function().addGood(c)){
				msg = "添加成功！请尽快结算，以免菜品库存不足。";
			}else
				msg = "添加失败！库存不足！";
		}
		
		request.setAttribute("msg", msg);
		request.getRequestDispatcher("AllGoods_Servlet").forward(request, response);
		
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
